#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "tformdoc.h"

#include <QMdiSubWindow>
#include <QFileDialog>
#include <cassert>
#include <QMdiSubWindow>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setCentralWidget(ui->mdiArea);

    ui->actCut->setEnabled(false);
    ui->actCopy->setEnabled(false);
    ui->actPaste->setEnabled(false);
    ui->actFont->setEnabled(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actDoc_New_triggered()
{
    TFormDoc *formDoc = new TFormDoc();
    ui->mdiArea->addSubWindow(formDoc);
    formDoc->show();

    ui->actCut->setEnabled(true);
    ui->actCopy->setEnabled(true);
    ui->actPaste->setEnabled(true);
    ui->actFont->setEnabled(true);
}


void MainWindow::on_actDoc_Open_triggered()
{
    bool needNew(true);
    TFormDoc *formDoc = nullptr;

    if (ui->mdiArea->subWindowList().size() > 0) {
        formDoc = dynamic_cast<TFormDoc *>(ui->mdiArea->activeSubWindow()->widget());
        needNew = formDoc->isFileOpened(); // 如果窗口已打开一个文件, 则需要新建一个窗口
    }

    QString curPath = QCoreApplication::applicationDirPath();
    QString fileName = QFileDialog::getOpenFileName(this, "打开一个文件", curPath,
                        "C/C++程序文件(*.h *.c *.cpp);;文本文件(*.txt);;所有文件(*.*)");
    if (fileName.isEmpty()) {
        return;
    }

    if (needNew) {
        formDoc = new TFormDoc(this);
        ui->mdiArea->addSubWindow(formDoc);
    }

    assert(formDoc != nullptr);
    formDoc->loadFromFile(fileName);
    formDoc->show();

    ui->actCut->setEnabled(true);
    ui->actCopy->setEnabled(true);
    ui->actPaste->setEnabled(true);
    ui->actFont->setEnabled(true);
}


void MainWindow::on_actDoc_Save_triggered()
{
    TFormDoc *formDoc = dynamic_cast<TFormDoc *>(ui->mdiArea->activeSubWindow()->widget());
    formDoc->saveToFile();
}


void MainWindow::on_actFont_triggered()
{
    TFormDoc *formDoc = dynamic_cast<TFormDoc *>(ui->mdiArea->activeSubWindow()->widget());
    formDoc->setEditFont();
}


void MainWindow::on_actCut_triggered()
{
    TFormDoc *formDoc = dynamic_cast<TFormDoc *>(ui->mdiArea->activeSubWindow()->widget());
    formDoc->textCut();
}


void MainWindow::on_actCopy_triggered()
{
    TFormDoc *formDoc = dynamic_cast<TFormDoc *>(ui->mdiArea->activeSubWindow()->widget());
    formDoc->textCopy();
}


void MainWindow::on_actPaste_triggered()
{
    TFormDoc *formDoc = dynamic_cast<TFormDoc *>(ui->mdiArea->activeSubWindow()->widget());
    formDoc->textPaste();
}


void MainWindow::on_actViewMode_triggered(bool checked)
{
    QMdiArea::ViewMode mode = checked ? QMdiArea::TabbedView : QMdiArea::SubWindowView;
    ui->mdiArea->setViewMode(mode);

    ui->mdiArea->setTabsClosable(checked);
    ui->actCascade->setEnabled(!checked);
    ui->actTile->setEnabled(!checked);
}


void MainWindow::on_actCascade_triggered()
{
    ui->mdiArea->cascadeSubWindows();
}


void MainWindow::on_actTile_triggered()
{
    ui->mdiArea->tileSubWindows();
}


void MainWindow::on_actCloseAll_triggered()
{
    ui->mdiArea->closeAllSubWindows();
}


void MainWindow::on_mdiArea_subWindowActivated(QMdiSubWindow *arg1)
{
    Q_UNUSED(arg1);
    if (ui->mdiArea->subWindowList().isEmpty()) {
        ui->actCut->setEnabled(false);
        ui->actCopy->setEnabled(false);
        ui->actPaste->setEnabled(false);
        ui->actFont->setEnabled(false);
    }
}

